<?php

class preFilter extends sfFilter {
	
	public function execute($filterChain) {

		$request = $this->getContext()->getRequest();
		
		$c = new Criteria();
		$parent_id = $request->getParameter('parent');

		if(!$parent_id)
		{
			$c->add(CategoriesPeer::PARENT_ID, null, Criteria::ISNULL);
			$categories = CategoriesPeer::doSelect($c);
		}
		else
		{
  	  $c->add(CategoriesPeer::PARENT_ID, $parent_id);
  	  $categories = CategoriesPeer::doSelect($c);
		}
		
		if($parent_id && !$categories)
		{
		  $parent_id = $this->getContext()->getUser()->getAttribute('parent');
		  $c->add(CategoriesPeer::PARENT_ID, $parent_id);
      $categories = CategoriesPeer::doSelect($c);	
		}
		
		$this->getContext()->getUser()->setAttribute('parent', $parent_id);
		
		$this->getContext()->getRequest()->setAttribute('categories', $categories);

		//execute filter
    $filterChain->execute();
	}
}
?>